package com.etalk.crm.serviceImpl;

import com.etalk.crm.dao.ClassBookingAutoRecordMapper;
import com.etalk.crm.dao.LessonsMapper;
import com.etalk.crm.pojo.ClassBookingAutoRecord;
import com.etalk.crm.pojo.ClassChangeAutoRecord;
import com.etalk.crm.pojo.LessonResult;
import com.etalk.crm.service.ClassBookingAutoRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.*;

/**
 * @author Jordan
 */
@Service
public class ClassBookingAutoRecordServiceImpl implements ClassBookingAutoRecordService {

    @Resource
    private ClassBookingAutoRecordMapper classBookingAutoRecordMapper;
    @Resource
    private LessonsMapper lessonsMapper;

    @Resource
    private HttpSession session;

    @Override
    public PageInfo<ClassBookingAutoRecord> searchClassBookingAutoRecordListByPage(ClassBookingAutoRecord record, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            pageNum=1;
        }
        if (pageSize == null){
            pageSize=20;
        }

        PageHelper.startPage(pageNum,pageSize);

        List<ClassBookingAutoRecord> list=classBookingAutoRecordMapper.selectClassBookingAutoRecordList(record);

        return new PageInfo<>(list);
    }

    @Override
    public Map<String, Object> addClassBookingAutoRecord(ClassBookingAutoRecord record) {
        Map<String,Object> resultMap=new HashMap<>(2);
        if (record==null){
            resultMap.put("status",0);
            resultMap.put("errMsg","添加数据为空");
            return resultMap;
        }
        if (record.getStudentId()==null){
            resultMap.put("status",0);
            resultMap.put("errMsg","没有学员");
            return resultMap;
        }
        if (StringUtils.isEmpty(record.getOrderId())){
            resultMap.put("status",0);
            resultMap.put("errMsg","没有订单");
            return resultMap;
        }

        if (record.getClassTime()==null){
            resultMap.put("status",0);
            resultMap.put("errMsg","缺少上课时间");
            return resultMap;
        }
        if(lessonsMapper.countLessonByPersonId(record.getStudentId(),record.getClassTime())>0){
            resultMap.put("status",0);
            resultMap.put("errMsg","已有预约课程");
            return resultMap;
        }
        record.setCreateTime(new Date()).setCreator(session.getAttribute("loginname").toString());
        if (classBookingAutoRecordMapper.countClassBookingAutoRecord(record)>0){
            resultMap.put("status",0);
            resultMap.put("errMsg","已加入约课计划");
            return resultMap;
        }
        record.setLessonId(0).setTeacher("");
        int num=classBookingAutoRecordMapper.insertClassBookingAutoRecord(record);
        if (num > 0){
            resultMap.put("status",1);
        }else {
            resultMap.put("status",0);
            resultMap.put("errMsg","添加约课计划记录失败");
        }
        return resultMap;
    }

    /**
     * 取消调课计划
     * @param id 调课计划id
     * @return
     */
    @Override
    public Map<String, Object> cancelClassBookingAutoRecordById(int id){
        Map<String,Object> resultMap=new HashMap<>(2);
        List<Integer> listId=new ArrayList<>();
        listId.add(id);
        int num=classBookingAutoRecordMapper.delClassBookingAutoRecordByIds(listId);
        if (num>0){
            resultMap.put("status",1);
        }else {
            resultMap.put("status",0);
            resultMap.put("errMsg","删除约课计划记录失败");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> cancelClassBookingAutoRecordById(List<Integer> listId) {
        Map<String,Object> resultMap=new HashMap<>(2);
        int num=classBookingAutoRecordMapper.delClassBookingAutoRecordByIds(listId);
        if (num>0){
            resultMap.put("status",1);
            resultMap.put("msg", "删除约课计划记录成功" + num + ",失败"+(listId.size()-num));
        }else {
            resultMap.put("status",0);
            resultMap.put("errMsg","删除约课计划记录失败");
        }
        return resultMap;
    }
}
